package com.cn.xiaonuo.slbfinances.shopassets.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cn.xiaonuo.common.constant.Status;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.ExportExcelUtils;
import com.cn.xiaonuo.common.vo.ExcelData;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.shopassets.entity.SlRevenueDetails;
import com.cn.xiaonuo.slbfinances.shopassets.param.PageRefundDetailParam;
import com.cn.xiaonuo.slbfinances.shopassets.rsp.RefundDetailRsp;
import com.cn.xiaonuo.slbfinances.shopassets.rsp.RevenueDetailsRsp;
import com.cn.xiaonuo.slbfinances.shopassets.rsp.ShopRefundDetailRsp;
import com.cn.xiaonuo.slbfinances.shopassets.service.SlRefundDetailService;
import com.cn.xiaonuo.slbfinances.slborders.mapper.SlOrderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: slb-reports-java
 * @Author: likui
 * @CreateTime: 2022-12-23  09:26
 * @Description: TODO
 */
@Service
public class SlRefundDetailServiceImpl implements SlRefundDetailService {
    @Autowired
    private SlOrderMapper slOrderMapper;

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage pageRefundDetail(PageRefundDetailParam param) {
        BusinessMessage message = new BusinessMessage();
        ShopRefundDetailRsp rsp = new ShopRefundDetailRsp();
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        List<RefundDetailRsp> pageRefundDetail = slOrderMapper.pageRefundDetail(param);
        PageInfo<RefundDetailRsp> pageInfo = new PageInfo<>(pageRefundDetail);
        rsp.setPageInfo(pageInfo);
        //订单合计金额
        ShopRefundDetailRsp sumOrderAmount=slOrderMapper.sumOrderAmount(param);
        rsp.setSumExpenditure(sumOrderAmount.getSumExpenditure());
        rsp.setSumGwq(sumOrderAmount.getSumGwq());
        Map<String,Object> sumWechatExpenditure=slOrderMapper.sumWechatExpenditure(param);
        rsp.setSumWechatExpenditure(Convert.toBigDecimal(sumWechatExpenditure.get("sumWechatExpenditure")));
        Map<String,Object> sumAlipayExpenditure=slOrderMapper.sumAlipayExpenditure(param);
        rsp.setSumAlipayExpenditure(Convert.toBigDecimal(sumAlipayExpenditure.get("sumAlipayExpenditure")));
        message.setData(rsp);
        message.setMessage(Status.success);
        message.setSuccess(true);
        message.setCode(Status.failCode);
        return message;
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public void pageRefundDetailExcel(HttpServletResponse response, PageRefundDetailParam param) {
        SimpleDateFormat ymdhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<RefundDetailRsp> pageRefundDetail = slOrderMapper.pageRefundDetail(param);
        ShopRefundDetailRsp sumOrderAmount=slOrderMapper.sumOrderAmount(param);
        Map<String,Object> sumWechatExpenditure=slOrderMapper.sumWechatExpenditure(param);
        Map<String,Object> sumAlipayExpenditure=slOrderMapper.sumAlipayExpenditure(param);
        try {
            List<List<Object>> rows = new ArrayList();
            ExcelData data = new ExcelData();
            //sheet表名称
            data.setName("商家退款明细导出");
            List<String> firstTitles=new ArrayList<>();
            firstTitles.add("合计支出金额汇总");
            firstTitles.add(Convert.toStr(sumOrderAmount.getSumExpenditure()));
            firstTitles.add("积分支出汇总");
            firstTitles.add(Convert.toStr(sumOrderAmount.getSumGwq()));
            firstTitles.add("支付宝支出汇总");
            firstTitles.add(Convert.toStr(sumAlipayExpenditure.get("sumAlipayExpenditure")));
            firstTitles.add("微信支出汇总");
            firstTitles.add(Convert.toStr(sumWechatExpenditure.get("sumWechatExpenditure")));
            data.setTitles(firstTitles);
            //表头
            List<Object> titles = new ArrayList();
            titles.add("编号");
            titles.add("店铺名称");
            titles.add("申请时间");
            titles.add("退款订单编号");
            titles.add("商家类型");
            titles.add("订单来源");
            titles.add("商家用户ID");
            titles.add("订单总金额");
            titles.add("购物券金额");
            titles.add("现金金额");
            titles.add("退款金额");
            titles.add("退款状态");
            titles.add("退款到");
            titles.add("退款时间");
            rows.add(titles);
            for (RefundDetailRsp detailRsp : pageRefundDetail) {
                List<Object> row1 = new ArrayList();
                row1.add(detailRsp.getId());
                row1.add(detailRsp.getShopName());
                String applicationTime = ymdhms.format(detailRsp.getApplicationTime());
                row1.add(applicationTime);
                row1.add(detailRsp.getRefundSerialNumber());
                if (ObjectUtils.isNotEmpty(detailRsp.getSlShopType())) {
                    if (detailRsp.getSlShopType().equals(0)) {
                        row1.add("普通店铺");
                    } else if (detailRsp.getSlShopType().equals(1)) {
                        row1.add("普通加盟店铺");
                    } else if (detailRsp.getSlShopType().equals(2)) {
                        row1.add("运营中心店铺");
                    } else if (detailRsp.getSlShopType().equals(3)) {
                        row1.add("厂家店铺");
                    }
                }
                if (detailRsp.getIsPubGoods().equals(0)){
                    row1.add("普通商品");
                }else if (detailRsp.getIsPubGoods().equals(1)){
                    row1.add("一县一品商品");
                }
                row1.add(detailRsp.getOwnerId());
                row1.add(detailRsp.getTotal());
                row1.add(detailRsp.getTotalGWJ());
                row1.add(detailRsp.getAgreementRefund());
                row1.add(detailRsp.getActualRecund());
                if (detailRsp.getFinanceCheckState().equals(0)){
                    row1.add("退款中");
                }else if (detailRsp.getFinanceCheckState().equals(1)){
                    row1.add("退款完成");
                }
                if (detailRsp.getPaymentChannel().equals(1)){
                    row1.add("微信");
                }else if (detailRsp.getPaymentChannel().equals(2)){
                    row1.add("支付宝");
                } else if (detailRsp.getPaymentChannel().equals(5)){
                    row1.add("线下退款");
                }else if (detailRsp.getPaymentChannel().equals(10)){
                    row1.add("购物劵");
                }else if (detailRsp.getPaymentChannel().equals(11)){
                    row1.add("支付宝+购物劵");
                }else if (detailRsp.getPaymentChannel().equals(12)){
                    row1.add("微信+购物劵");
                }else if (detailRsp.getPaymentChannel().equals(15)){
                    row1.add("微信+线下");
                }else if (detailRsp.getPaymentChannel().equals(16)){
                    row1.add("支付宝+线下");
                }else if (detailRsp.getPaymentChannel().equals(17)){
                    row1.add("银联");
                }else if (detailRsp.getPaymentChannel().equals(18)){
                    row1.add("银联+购物券");
                }else if (detailRsp.getPaymentChannel().equals(101)){
                    row1.add("好收银支付宝");
                }else if (detailRsp.getPaymentChannel().equals(102)){
                    row1.add("好收银微信");
                }else if (detailRsp.getPaymentChannel().equals(103)){
                    row1.add("好收银云闪付");
                }else {
                    row1.add("其它");
                }
                if (ObjectUtils.isNotEmpty(detailRsp.getFinanceCheckTime())){
                    String financeCheckTime = ymdhms.format(detailRsp.getFinanceCheckTime());
                    row1.add(financeCheckTime);
                }else {
                    row1.add("");
                }
                rows.add(row1);
            }
            data.setRows(rows);
            ExportExcelUtils.exportExcel(response, "商家退款明细导出.xlsx", data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
